package cn.dada.dadarecipes.base.exception;

import cn.dada.dadarecipes.base.response.JsonResult;
import cn.dada.dadarecipes.base.response.StatusCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.validation.ConstraintViolationException;

@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    /**
     * 用于处理 IllegalArgumentException 异常
     * @param ex IllegalArgumentException
     * @return
     * @see java.lang.IllegalArgumentException
     */
    @ExceptionHandler
    public JsonResult doHandlerIllegalArgumentException(IllegalArgumentException ex){
        String data = ex.getMessage();
        log.error("IllegalArgumentException:{}",data);
        return JsonResult.fail(data);
    }

    /**
     * 校验POJO类dto数据bind异常
     * @param ex
     * @return
     * @see org.springframework.validation.BindException
     */
    @ExceptionHandler
    public JsonResult doHandlerBindException(BindException ex){
        String data = ex.getFieldError().getDefaultMessage();
        log.error("BindException:{}",data);
        return new JsonResult(StatusCode.VALIDATED_ERROR,data);
    }

    /**
     * 校验POJO类dto数据异常
     * 用于处理 MethodArgumentNotValidException 异常
     * @param ex MethodArgumentNotValidException
     * @return
     * @see org.springframework.web.bind.MethodArgumentNotValidException
     */
    @ExceptionHandler
    public JsonResult doHandlerMethodArgumentNotValidException(MethodArgumentNotValidException ex){
        String data =ex.getFieldError().getDefaultMessage();
        log.error("MethodArgumentNotValidException:{}",data);
        return new JsonResult(StatusCode.VALIDATED_ERROR,data);
    }

    /**
     * 校验非POJO类参数数据异常
     * 用于处理 ConstraintViolationException 异常
     * @param ex ConstraintViolationException
     * @return
     * @see javax.validation.ConstraintViolationException
     */
    @ExceptionHandler
    public JsonResult doHandlerConstraintViolationException(ConstraintViolationException ex){
        String[] data = ex.getMessage().split(": ");
        log.error("ConstraintViolationException:{}",data[1]);
        return new JsonResult(StatusCode.VALIDATED_ERROR,data[1]);
    }




    /**
     * 用于处理自定义 ServiceException 异常
     * @param ex ServiceException
     * @return
     * @see cn.dada.dadarecipes.base.exception.ServiceException
     */
    @ExceptionHandler
    public JsonResult doHandlerServiceException(ServiceException ex){
        String data = ex.getMessage();
        log.error("ServiceException:ex={}",ex.getStatusCode().getMsg());
        return new JsonResult(ex.getStatusCode(),data);
    }





    //所有自定义异常一定要加在 doHandlerRuntimeException 和 doHandlerThrowableException 之前
    /**
     * 用于处理 RuntimeException 异常
     * @param ex RuntimeException
     * @return
     * @see java.lang.RuntimeException
     */
    @ExceptionHandler
    public JsonResult doHandlerRuntimeException(RuntimeException ex){
        String data = ex.getMessage();
        log.error("RuntimeException:{}",data);
        return JsonResult.fail(data);
    }
    /**
     * 用于处理 Throwable 异常
     * @param ex Throwable
     * @return
     * @see java.lang.Throwable
     */
    @ExceptionHandler
    public JsonResult doHandlerThrowableException(Throwable ex){
        String data = ex.getMessage();
        log.error("Throwable:{}",data);
        return new JsonResult(StatusCode.THROW_ERR0R,data);
    }
}
